package com.henry.factory;

import com.henry.encryptor.Encryptor;
import jakarta.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 加解密工具类 工厂
 *
 * @author Liu Hanlin
 * @create 2025-08-16 11:50
 */
@Component
@AllArgsConstructor
public class EncryptorFactory {

    private final Map<String, Encryptor> encryptorMap = new HashMap<>();

    private final ApplicationContext applicationContext;

    @PostConstruct
    public void init(){

        Map<String, Encryptor> encryptors = applicationContext.getBeansOfType(Encryptor.class);
        encryptorMap.putAll(encryptors);
    }

    /**
     * 获取加解密工具类
     *
     * @param encryptorType 加解密类型
     * @return 具体加解密工具类
     */
    public Encryptor getEncryptor(String encryptorType){
        return encryptorMap.get(encryptorType);
    }
}
